package graphics;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import controller.Constants;

/**
 * Line
 * 
 * @author Jie Zhao(288654)
 * 
 */
public class Line extends Shape {
	private Point start = new Point();
	private Point end = new Point();

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("line:");
		sb.append(getLineColor().getRGB()+":");
		sb.append((isFilled()?"1":"0") + ":");
		sb.append(getFilledColor().getRGB()+":");
		sb.append(getStroke().getLineWidth()+":");
		sb.append(getStart().x+","+getStart().y+":");
		sb.append(getEnd().x+","+getEnd().y);
		return sb.toString();
	}

	@Override
	public void fromString(String str) {
		try{
		String[] data = str.split(":");
		if(!data[0].equals("line"))return;
		setLineColor(new Color(Integer.parseInt(data[1])));
		setFilled(data[2].equals("1"));
		setFilledColor(new Color(Integer.parseInt(data[3])));
		setStroke(new BasicStroke(Float.parseFloat(data[4]),BasicStroke.CAP_ROUND,BasicStroke.CAP_ROUND));
		String[] point = data[5].split(",");
		setStart(new Point(Integer.parseInt(point[0]),Integer.parseInt(point[1])));
		point = data[6].split(",");
		setEnd(new Point(Integer.parseInt(point[0]),Integer.parseInt(point[1])));
		}catch(Exception e)
		{
			if(Constants.DEBUG)e.printStackTrace();
		}
	}
	@Override
	/**
	 * Draw a line
	 */
	public void draw(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setColor(lineColor);
		g2d.setStroke(getStroke());
		g.drawLine(start.x, start.y, end.x, end.y);
	}

	/**
	 * 
	 * @return the start point of the line
	 */
	public Point getStart() {
		return start;
	}

	/**
	 * set the start point of the line
	 * 
	 * @param start
	 */
	public void setStart(Point start) {
		this.start = start;
	}

	/**
	 * 
	 * @return the end point of the line
	 */
	public Point getEnd() {
		return end;
	}

	/**
	 * set the end point of the line
	 * 
	 * @param end
	 */
	public void setEnd(Point end) {
		this.end = end;
	}
	
	public static void main(String[] args) {
		Line l = new Line();
		l.setStart(new Point(123,234));
		l.setEnd(new Point(222,333));
		System.out.println(l);
		
		Line l2 = new Line();
		l2.fromString(l.toString());
		System.out.println(l2);
	}
}
